package com.song.studentddb.controller;


import com.song.student_demo.pojo.Student;
import com.song.student_demo.service.StudentService;
import com.song.student_demo.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

@Controller
@Slf4j
@RequestMapping("/sys/user")
public class indexController {
    @Resource
    private StudentService studentService;

    @GetMapping("/index")
    public  String index(){
        System.out.println("=========");
        return "index";
    }
    @GetMapping("/list/{pageNo}/{pageSize}")
    @ResponseBody
    public ResultVo getUserList(@PathVariable("pageNo")Integer pageNo, @PathVariable("pageSize")Integer pageSize){
        log.info("【分页列表】");
        return ResultVo.success(studentService.getPageList(pageNo,pageSize));
    }

    @PostMapping("/update")
    @ResponseBody
    public  ResultVo updateUser(@RequestBody Student student){
        log.info("【进入学生成绩修改控制层】");
        System.out.println(student);
        try {
            studentService.update(student);
            return ResultVo.success("【学生成绩修改成功！】");
        }catch (Exception e){
            e.printStackTrace();
            return ResultVo.fail("【学生成绩修改失败！】");
        }
    }
    @ResponseBody
    @PostMapping("/add")
    public ResultVo addUser(@RequestBody Student student){
        try{
            student.setSname("张三");
            student.setSubject("java");
            studentService.save(student);
            System.out.println("添加学生成绩"+student);
            return ResultVo.success("添加学生成绩成功!");
        }catch (Exception e){
            e.printStackTrace();
            return  ResultVo.fail("添加学生成绩失败",e);
        }
    }
    @ResponseBody
    @GetMapping("/del")
    public ResultVo  del(String sid){
        System.out.println("==========");
        try {
            System.out.println(sid);
            studentService.delete(Integer.parseInt(sid));
            return ResultVo.success("删除学生成绩成功！");
        }catch (Exception e){
            e.printStackTrace();
            return ResultVo.fail("删除学生成绩",e);
        }
    }
}

